package com.wkck.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wkck.VO.CompanyAllInfoListByIdsDto;
import com.wkck.VO.PageResult;
import com.wkck.domain.Company;
import com.wkck.dto.CompanyQueryParamsDto;
import com.wkck.dto.SaveCompanyInfoDto;
import com.wkck.result.Result;
import com.wkck.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: CK
 * @Date: 2023/12/25/11:59
 * @Description:
 */
@Api(tags = "入驻公司")
@Slf4j
@RequestMapping("/company")
@RestController
@CrossOrigin
public class CompanyBgController {

    @Autowired
    private CompanyService companyService;

    /**
    * @Author: CK
    * @Date: 2023/12/25
    * @Param: []
    * @return: 所有公司信息
    * @Description:
    */
    @GetMapping
    @ApiOperation(value = "得到所有公司简易信息")
    public Result<List<Company>> getAllCompany() {
        log.info("得到所有公司简易信息");

        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getState, 0);

        return Result.success(companyService.list(queryWrapper));
    }

    /**
    * @Author: CK
    * @Date: 2023/12/25
    * @Param: []
    * @return:
    * @Description: 根据Id查询公司信息
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据Id查询公司信息")
    public Result<Company> getCompanyInfoById(@PathVariable Integer id) {
        log.info("查询公司信息: {}", id);
        return Result.success(companyService.getById(id));
    }

    /**
    * @Author: CK
    * @Date: 2024/2/8
    * @Param: [companyQueryParamsDto]
    * @Description: 得到所有公司完整信息
    */
    @PostMapping
    @ApiOperation(value = "得到所有公司完整信息")
//    @Cacheable(value = "getAllCompanyInfo")
    public Result<PageResult> getAllCompanyInfo(@RequestBody CompanyQueryParamsDto companyQueryParamsDto) {
        log.info("查询所有公司信息 参数：{}", companyQueryParamsDto);
        return Result.success(companyService.getAllCompanyInfo(companyQueryParamsDto));
    }

    /**
    * @Author: CK
    * @Date: 2024/2/9
    * @Param: []
    */
    @GetMapping("/clearCache")
    @ApiOperation(value = "清理缓存")
    public Result<Boolean> clearCache() {
        log.info("清理缓存任务执行");
        return Result.success(companyService.clearCache());
    }

    /**
    * @Author: CK
    * @Date: 2024/2/10
    * @Param: [saveCompanyInfoDto]
    * @Description: 保存公司信息
    */
    @PostMapping("/save")
    @ApiOperation(value = "保存公司信息")
    public Result<Boolean> saveCompanyInfo(@RequestBody SaveCompanyInfoDto saveCompanyInfoDto) {
        log.info("保存公司信息: {}", saveCompanyInfoDto);
        return Result.success(companyService.saveCompanyInfo(saveCompanyInfoDto));
    }

    /**
    * @Author: CK
    * @Date: 2024/2/11
    * @Description: 根据id删除公司信息
    */
    @DeleteMapping("/removeByIds")
    @ApiOperation(value = "根据id删除公司信息")
    public Result<Boolean> removeByIds(@RequestBody List<Integer> ids) {
        log.info("ids: {}", ids);
        return Result.success(companyService.removeCompanyInfoByIds(ids));
    }

    /**
     * @Author: CK
     * @Date: 2024/2/12
     * @Param: [saveCompanyInfoDto]
     * @Description: 修改公司信息
     */
    @PutMapping("/modifyCompanyInfo")
    @ApiOperation(value = "修改公司信息")
    public Result<Boolean> modifyCompanyInfo(@RequestBody SaveCompanyInfoDto saveCompanyInfoDto){
        log.info("修改公司信息: {}", saveCompanyInfoDto);
        return Result.success(companyService.modifyCompanyInfo(saveCompanyInfoDto));
    }

    /***
    * @Author: CK
    * @Date: 2024/2/12
    * @Param: [id, state]
    * @Description: 修改公司状态
    */
    @GetMapping("/updateState/{id}/{state}")
    @ApiOperation(value = "修改公司状态")
    public Result<Boolean> updateCompanyState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
        log.info("{}, 需要修改的状态: {}", id, state);
        return Result.success(companyService.updateCompanyState(id, state));
    }

    /***
    * @Author: CK
    * @Date: 2024/2/12
    * @Param: [params]
    * @Description: 公司信息详情
    */
    @PostMapping("/companyInfoByIds")
    @ApiOperation(value = "公司信息详情")
    public Result<List<CompanyAllInfoListByIdsDto>> companyInfoByIds(@RequestBody Map<String,List<Integer>> params) {
        List<Integer> ids = params.get("ids");
        log.info("ids: {}", ids);
        return Result.success(companyService.getCompanyInfoByIds(ids));
    }

}
